package org.csu.myblogger.myblogger.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.Getter;
import org.csu.myblogger.myblogger.commom.CommonResponce;
import org.csu.myblogger.myblogger.entity.Comment;
import org.csu.myblogger.myblogger.entity.WaitComment;
import org.csu.myblogger.myblogger.mapper.*;
import org.csu.myblogger.myblogger.service.service.CommentService;
import org.csu.myblogger.myblogger.vo.CommentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Getter
@Service("commentService")
public class CommentServiceImpl implements CommentService {
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private ArticleKindsMapper articleKindsMapper;
    @Autowired
    private ArticleStartMapper articleStartMapper;
    @Autowired
    private WaitCommentMapper waitCommentMapper;

    @Override
    public CommonResponce PublishComment(String username, CommentVo commentVo) {
        WaitComment waitComment=new WaitComment();
        BeanUtils.copyProperties(commentVo,waitComment);
        waitComment.setReviewer_id(username);
        try{
            waitCommentMapper.insert(waitComment);
            return CommonResponce.CreatForSuccess("评论成功等待审核");//等待审核以后再写吧
        }catch (RuntimeException e){
            System.out.println(new RuntimeException(e));
            return CommonResponce.createForError(500,"评论失败");
        }
    }

    @Override
    public CommonResponce DeleteComment(String username, CommentVo commentVo) {
        WaitComment waitComment=new WaitComment();
        BeanUtils.copyProperties(commentVo,waitComment);
        waitComment.setReviewer_id(username);
        QueryWrapper<WaitComment>queryWrapper=new QueryWrapper<>(waitComment);
        try{
            waitCommentMapper.delete(queryWrapper);
            return CommonResponce.CreatForSuccess("删除评论成功");//等待审核以后再写吧
        }catch (RuntimeException e){
            System.out.println(new RuntimeException(e));
            return CommonResponce.createForError(500,"删除评论失败");
        }
    }

    //审核评论
    @Override
    public CommonResponce<List<WaitComment>> AuditComment(String username) {
        //假设已经有管理员权限
        try {
            QueryWrapper<WaitComment>waitCommentQueryWrapper=new QueryWrapper<>();
            List<WaitComment> waitCommentList=waitCommentMapper.selectList(waitCommentQueryWrapper);
            return CommonResponce.CreatForSuccess("推送成功",waitCommentList);
        }catch (RuntimeException e){
            System.out.println(new RuntimeException(e));
            return CommonResponce.createForError(500,"评论推送失败");
        }
    }

    //审核结束，更新审核
    @Override
    public CommonResponce UpdateCommemt(String username, List<Comment> comments) {
        try{
            commentMapper.insert(comments);
            return CommonResponce.CreatForSuccess("审核成功");
        }catch (RuntimeException e){
            System.out.println(new RuntimeException(e));
            return CommonResponce.createForError(500,"评论审核失败");
        }
    }
}
